package com.gdpu.liou.symphonyserver.controller;


import com.alibaba.fastjson.JSONObject;
import com.gdpu.liou.symphonyserver.common.aspect.WebLog;
import com.gdpu.liou.symphonyserver.common.utils.Consts;
import com.gdpu.liou.symphonyserver.entity.Song;
import com.gdpu.liou.symphonyserver.entity.SongList;
import com.gdpu.liou.symphonyserver.entity.respose.Response;
import com.gdpu.liou.symphonyserver.entity.respose.ResponseCode;
import com.gdpu.liou.symphonyserver.service.SongListService;
import com.gdpu.liou.symphonyserver.common.utils.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @author liou
 */
@Slf4j
@RestController
@RequestMapping(value = "/songList")
public class SongListController {
    private final SongListService songListService;

    public SongListController(SongListService songListService) {
        this.songListService = songListService;
    }

    /**
     * 通过歌单Id查询歌单详情
     */
    @RequestMapping(value = "/playlistById", method = RequestMethod.GET)
    public Response songListById(@RequestParam Long id) {
            SongList songList = songListService.songListById(id);
            if (songList != null) {
                return new Response<>(songList);
            }
            return new Response<>();
    }

    /**
     * 查询歌单分页数据
     */
    @RequestMapping(value = "/pagingQuerySongList", method = RequestMethod.GET)
    public PagingUtil pagingQuerySinger(Integer currentPage, String title) {
        if (currentPage == null || currentPage == 0) {
            currentPage = 1;
        }
        return songListService.pagingQuerySongList(currentPage, title);
    }

    /**
     * 查询全部歌单数据
     */
    @RequestMapping(value = "/selectAllSongList", method = RequestMethod.GET)
    public List<SongList> selectAllSongList() {
        return songListService.selectAllSongList();
    }


    /**
     * 根据标题模糊查询歌单列表
     */
    @RequestMapping(value = "/likeTitle", method = RequestMethod.GET)
    public List<SongList> likeTitle(@RequestParam String title) {
        return songListService.likeTitle("%" + title + "%");
    }


    /**
     * 根据歌单风格类型获取歌单列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/likeStyle", method = RequestMethod.GET)
    public Response likeStyle(HttpServletRequest request) {
        try {
            // 获取歌单风格类型
            String style = request.getParameter("style");
            List<SongList> songLists = songListService.likeStyle("%" + style + "%");
            return new Response<>(songLists);
        } catch (Exception e) {
            log.error("请求根据歌单风格类型获取歌单列表接口失败", e);
            return new Response<>(-1, "系统内部错误：请求根据歌单风格类型获取歌单列表接口失败");
        }
    }


    /**
     * 查询评分高歌单列表
     */
    @RequestMapping(value = "/goodSongList", method = RequestMethod.GET)
    public List<SongList> goodSongList() {
        return songListService.selectGoodSongList();
    }

    /**
     * 根据歌单名模糊查询歌单列表
     */
    @WebLog(description = "请求歌单列表-根据歌单名模糊查询")
    @RequestMapping(value = "/songListByKeyword", method = RequestMethod.GET)
    public List<SongList> songListByKeyword(@RequestParam("keyword") String keyword,
                                            @RequestParam(value = "limit", required = false) Integer limit) {
        if (limit == null) {
            limit = 100;
        }
        return songListService.songListByKeyword(keyword, limit);
    }
    /**
     * 添加歌单
     */
    @RequestMapping(value="/add",method = RequestMethod.POST)
    public JSONObject addSongList(@RequestParam("title") String title,@RequestParam("introduction") String introduction,
                                  @RequestParam("style")String style, @RequestParam("pic")MultipartFile pic) throws IOException {
        SongList newSongList = new SongList();
        newSongList.setTitle(title);
        newSongList.setIntroduction(introduction);
        newSongList.setStyle(style);
        System.out.println( songListService.addSongList(newSongList,pic));;

        JSONObject jsonObject= new JSONObject();
        jsonObject.put(Consts.CODE, 1);
        jsonObject.put(Consts.MSG, "添加成功");
        return jsonObject;
    }
    /**
     * 删除歌单
     */
    @GetMapping("/deleteSongList")
    public JSONObject deleteSongList(String id){
        JSONObject jsonObject = new JSONObject();
        //删除歌手
        boolean flag = songListService.deleteSongList(Integer.parseInt(id));
        //删除成功
        if (flag) {
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "删除成功");
            return jsonObject;
        }
        //删除失败
        jsonObject.put(Consts.CODE, 0);
        jsonObject.put(Consts.MSG, "删除失败");
        return jsonObject;
    }
    /**
     * 修改歌单
     */
    @PostMapping("/update")
    public JSONObject updateSongList(@RequestParam("id")String id,@RequestParam("title") String title,@RequestParam("introduction") String introduction,
                                     @RequestParam("style")String style, MultipartFile pic) throws IOException {
        JSONObject jsonObject = new JSONObject();
        SongList songList= songListService.songListById(Long.valueOf(id));
        if(songList==null){
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "修改失败");
        }else{
            songList.setTitle(title);
            songList.setIntroduction(introduction);
            songList.setStyle(style);
            if (songListService.updateSongList(songList,pic)) {
                jsonObject.put(Consts.CODE, 1);
                jsonObject.put(Consts.MSG, "修改成功");
            }else{
                jsonObject.put(Consts.CODE, 0);
                jsonObject.put(Consts.MSG, "修改失败");
            }

        }
        return jsonObject;
    }
}
